package com.ph.clouds.tracker.config;

import android.content.Context;

import com.ph.clouds.tracker.db.TrackDatabaseHelper;
import com.ph.clouds.tracker.entity.TrackDevice;
import com.ph.clouds.tracker.strategy.TrackerUploadStrategy;
import com.ph.clouds.tracker.util.TrackerLog;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yushuangjiang
 * @Time 2021/8/2 12:06
 * @Description： 埋点配置类
 */
public class TrackConfiguration {

    /**
     * 是否显示LOG
     */
    private boolean enableLog;

    /**
     * 服务器上传统计数据接口地址
     */
    private String serverUrl;

    /**
     * 单次最多上传20条   TODO ：sql 查询添加查询limit条件
     */
    private int oneceUploadLimit = 20;

    /**
     * 默认1分钟上传一次
     */
    private int mMinutes = 1;

    /**
     * 设备信息
     */
    private TrackDevice device;

    /**
     * 上传策略 {@link TrackerUploadStrategy}
     */
    private TrackerUploadStrategy uploadStrategy;

    private Map<String, String> headerMap = new HashMap<>();

    /**
     * 设置网络请求需要的Header
     */
    public TrackConfiguration setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
        return this;
    }


    public Map<String, String> getHeaderMap() {
        return headerMap;
    }


    public TrackConfiguration() {
        enableLog = false; //默认
        uploadStrategy = TrackerUploadStrategy.NOW; //默认
    }

    public TrackConfiguration enableLog(boolean enableLog) {
        this.enableLog = enableLog;
        TrackerLog.setEnableLog(enableLog);
        return this;
    }


    /**
     * 初始化缓存数据库
     */
    public TrackConfiguration initializeDb(Context context) {
        TrackDatabaseHelper.getInstance(context);
        return this;
    }

    public int getOneceUploadLimit() {
        return oneceUploadLimit;
    }

    public void setOneceUploadLimit(int oneceUploadLimit) {
        this.oneceUploadLimit = oneceUploadLimit;
    }


    /**
     * 设置上传 分钟间隔数
     */
    public TrackConfiguration setMinutes(int minutes) {
        if (minutes > 60) {
            mMinutes = 60;
        } else {
            this.mMinutes = minutes;
        }
        return this;
    }


    public TrackConfiguration setServerUrl(String uploadUrl) {
        this.serverUrl = uploadUrl;
        return this;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    /**
     * 设置上传策略
     */
    public TrackConfiguration setUploadCategory(TrackerUploadStrategy uploadCategory) {
        this.uploadStrategy = uploadCategory;
        return this;
    }

    public TrackDevice getDevice() {
        return device;
    }

    /**
     * 设置设备信息
     */
    public TrackConfiguration setDevice(TrackDevice device) {
        this.device = device;
        return this;
    }

    public boolean isenableLog() {
        return enableLog;
    }

    public TrackerUploadStrategy getUploadCategory() {
        return uploadStrategy;
    }


    public int getMinutes() {
        return mMinutes;
    }

}
